package ReflectDemo;

import java.awt.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

        //1.获取Class对象
        Class<Stu> stuClass = Stu.class;
        //Constructor<?>[] cons = stuClass.getConstructors();  //返回公共的构造方法
        Constructor<?>[] cons = stuClass.getDeclaredConstructors();  //返回所有构造方法
        for(Constructor con : cons)
            System.out.println(con);

        //2.通过Class对象获得构造方法
        //Constructor<ReflectDemo.Stu> con = stuClass.getConstructor(); //获取无参构造方法(公有方法)
        Constructor<Stu> con = stuClass.getDeclaredConstructor(String.class, int.class);
        //Constructor<ReflectDemo.Stu> con = stuClass.getConstructor(String.class, int.class,String.class);//获取带参构造方法(公有方法)

        //3.通过构造方法 创建对象
        //ReflectDemo.Stu obj = con.newInstance();
        Stu obj = con.newInstance("Mary", 24);
        //ReflectDemo.Stu obj = con.newInstance("Jack",20,"Beijing");
        System.out.println(obj);

    }
}
